package pojo.wizards;

import java.util.Arrays;
import java.util.Collections;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;

import pojo.utils.DbUtils;
import pojo.utils.I18n;
import pojo.utils.UiUtils;

public class TableWizardPage extends WizardPage {


	private List ordinalList;// 原始列表
	private List selectedList; // 已经选择的列表
	private Button allToRightButton;// 全部选择
	private Button toRightButton;// 选择
	private Button toLeftButton; // 取消选择
	private Button allToLeftButton; // 全部取消
	private java.util.List<String> listItems; // 数据库表

	protected TableWizardPage(String pageName) {
		super(pageName);
		setTitle("数据表信息");
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(3, false);
		container.setLayout(layout);
		layout.verticalSpacing = 10;
		layout.horizontalSpacing = 3;

		// UI

		// 1.左列表
		Group leftGroup = new Group(container, SWT.NULL);
		leftGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		leftGroup.setText(I18n.tableSelectWp_databaseTable);
		leftGroup.setLayout(new GridLayout(1, false));
		ordinalList = UiUtils.createOrdinalList(leftGroup);
		// 2.按钮
		{ // 列表中间的按钮组
			Composite btnGroup = new Composite(container, SWT.NULL);
			GridLayout btnGroupLayout = new GridLayout(1, false);
			btnGroup.setLayout(btnGroupLayout);
			btnGroupLayout.verticalSpacing = 9;
			allToRightButton = UiUtils.createAllToRightButton(this, btnGroup);
			toRightButton = UiUtils.createToRightButton(this, btnGroup);
			toLeftButton = UiUtils.createToLeftButton(this, btnGroup);
			allToLeftButton = UiUtils.createAllToLeftButton(this, btnGroup);
		}
		// 3.右列表
		Group rightGroup = new Group(container, SWT.NULL);
		rightGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		rightGroup.setText(I18n.tableSelectWp_selectedTable);
		rightGroup.setLayout(new GridLayout(1, false));
		selectedList = UiUtils.createSelectedList(rightGroup);
		setControl(container);
	}

	@Override
	public void setVisible(boolean visible) {
		beforeVisible();
		super.setVisible(visible);
	}

	private void beforeVisible() {
		String message = null;
		DatabaseWizardPage prevPage = (DatabaseWizardPage) getPreviousPage();
		listItems = DbUtils.getTables(prevPage.getDsModel());
		Collections.sort(listItems);
		listItems = getexcludedTables();
		getOrdinalList().setItems(listItems.toArray(new String[listItems.size()]));
		setErrorMessage(message);
		setPageComplete(message == null);
		if (message == null) {
			UiUtils.Validator.validateEmptyItem(this);
		}
	}

	public List getOrdinalList() {
		return ordinalList;
	}

	public List getSelectedList() {
		return selectedList;
	}

	public Button getAllToRightButton() {
		return allToRightButton;
	}

	public Button getToRightButton() {
		return toRightButton;
	}

	public Button getToLeftButton() {
		return toLeftButton;
	}

	public Button getAllToLeftButton() {
		return allToLeftButton;
	}

	public java.util.List<String> getListItems() {
		return listItems;
	}
	
	public java.util.List<String> getexcludedTables() {
		java.util.List<String> all = getListItems();
		String[] selectedTableNames = getSelectedList().getItems();
		java.util.List<String> selects = Arrays.asList(selectedTableNames);
		all.removeAll(selects);
		return all;
	}
}
